package mvc;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import action.LoginAction;
import action.SignAction;
import annotion.Testannotion;
import form.Action;
import form.ActionForm;
import form.Loginform;
import unti.FullBean;

/**
 * Servlet implementation class ActionServlet
 */
@WebServlet(urlPatterns = "/ActionServlet", asyncSupported = true)
//@WebServlet("/ActionServlet")
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//** 测试annotion //
		
		Class<LoginAction> c = LoginAction.class;
	
		if(c.isAnnotationPresent(Testannotion.class))
		{
			Testannotion inheritedTest = c.getAnnotation(Testannotion.class);
			
			String value = (String) inheritedTest.value();
			System.out.println(value);
		}

		
		Action form =  FullBean.full(request);
		ActionForm action = null ; //ActionForm 为 接口
		/*String aa = request.getParameter("sign");*/
		/*if(request.getParameter("sign").equals("form.Loginform"))
		{
			action =  new LoginAction();
			
			action.execute(form);
		}
		if(request.getParameter("sign").equals("form.Signform"))
		{
			action =  new SignAction();
			
			action.execute(form);
		}*/
		Map<String, String>map = ActionMapping.getmapping();
		String actionname = map.get(request.getParameter("sign"));
		Class<?> clazz;
		try {
			clazz = Class.forName(actionname);
			action = (ActionForm) clazz.newInstance();
			action.execute(form);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
